//
//  YKSearchNormalDataByGoodViewController.m
//  YunKang
//
//  Created by leoking870 on 2017/11/2.
//  Copyright © 2017年 leoking870. All rights reserved.
//

#import "YKSearchNormalDataByGoodViewController.h"
#import "YKDataHeaderView.h"
#import "YKMultiSelectDropdownView.h"
#import "YKSaleModel.h"
#import "YKClientListHeaderView.h"
#import "YKSelectDropdownView.h"
#import "YKSelectView.h"
#import "YKSaleItem.h"
#import "YKSearchViewController.h"
#import "YKPopupView.h"
#import "YKSearchOnlineViewController.h"
@interface YKSearchNormalDataByGoodViewController ()
@property (nonatomic, strong) YKSelectView *goodsSelectView;
@property (nonatomic, strong) YKDataHeaderView *headerView;
@property (nonatomic, strong) NSArray *normalSummaryDetail;
@end

@implementation YKSearchNormalDataByGoodViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"查询明细";
    self.view.backgroundColor = [UIColor whiteColor];
    @weakify(self)
    YKDataHeaderView *headerView = [[YKDataHeaderView alloc]initWithType:YKDataHeaderViewTypeYearMonthDay];
    headerView.dateChangeBlock = ^(NSString *start, NSString *end) {
        
        [self_weak_ loadData];
    };
    self.headerView = headerView;
    [self.view addSubview:headerView];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(self.view.mas_top);
    }];
    
    self.goodsSelectView = [[YKSelectView alloc]initWithTitle:@"选择品种" row:[CommonDataManager sharedInstance].goods initialText:[CommonDataManager sharedInstance].goods.firstObject];
    self.goodsSelectView.enabled = NO;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onSelectTapped:)];
    self.goodsSelectView.userInteractionEnabled = YES;
    [self.goodsSelectView addGestureRecognizer:tap];
    self.goodsSelectView.selectHandler = ^(id<YKDropdown> value) {
        [self loadData];
    };
    [self.view addSubview:self.goodsSelectView];
    [self.goodsSelectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(headerView.mas_bottom);
    }];
    
    YKClientListHeaderView* listHeaderView = [[YKClientListHeaderView alloc]initWithTitles:@[@"客户",@"销量",@"金额"] weights:@[@1,@1,@1]];
    listHeaderView.textFont = [UIFont systemFontOfSize:16];
    [self.view addSubview:listHeaderView];
    [listHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(self.goodsSelectView.mas_bottom);
    }];
    listHeaderView.backgroundColor = [UIColor tf_colorWithHexString:@"#1EADFC"];
    
    listHeaderView.textFont = [UIFont systemFontOfSize:16];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.bottom.trailing.equalTo(self.view);
        make.top.equalTo(listHeaderView.mas_bottom);
    }];
    [self loadData];
}
- (void)onSelectTapped:(id)sender {
    @weakify(self)
    YKSearchOnlineViewController *vc = [YKSearchOnlineViewController new];
    __weak YKSearchOnlineViewController *wvc = vc;
    vc.selectDrugComplete = ^(id<YKDropdown> model) {
        self_weak_.goodsSelectView.selectedRow = model;
        [YKPopupView dismissViewController:wvc];
    };
    vc.dataSource = [CommonDataManager sharedInstance].goods;
    
    [YKPopupView showPopupViewWithViewController:vc height:self.view.tf_height - 150];
}
- (void)loadData {
    [self tf_showStateView:kTFViewStateLoading inView:self.tableView];
    [TFHTTPMANAER POST:@"manage/normalSummaryDetail.htm" parameters:@{@"startDate":self.headerView.start, @"endDate": self.headerView.end, @"goodsId":_goodsSelectView.selectedRow.code?:@""} success:^(id responseObject) {
        
        [self tf_showStateView:kTFViewStateNone inView:self.tableView];
        [self.tableViewManager removeAllSections];
        self.normalSummaryDetail = [NSArray yy_modelArrayWithClass:[YKSaleModel class] json:responseObject[@"normalSummaryDetail"]];
        
        TFTableViewSection * section = [[TFTableViewSection alloc]init];
        section.headerHeight = .1f;
        section.footerHeight = .1f;
        [section addItemsFromArray:[self.normalSummaryDetail tf_mapUsingBlock:^id _Nullable(id  _Nonnull obj, NSInteger idx) {
            return [YKSaleItem itemWithModel:obj];
        }]];
        if (section.items.count == 0) {
            [self tf_showStateView:kTFViewStateNoData inView:self.tableView];
        }
        [self.tableViewManager addSection:section];
        
        [self.tableView reloadData];
    } failure:^(id responseObject, NSError *error) {
        [self tf_showStateView:kTFViewStateNetError inView:self.tableView];
    }];
}


@end
